package com.magicsoft.app.helper;

import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

public class ListViewHeightHepler {

	/**
	 * 重新计算ListView的高度
	 * 
	 * @param listView
	 */
	public static int setListViewHeightBasedOnChildren(ListView listView) {

		int totalHeight = 0;

		ListAdapter listAdapter = listView.getAdapter();
		int count = listAdapter.getCount();
		if (listAdapter != null && count > 0) {
			// pre-condition
			View listItem = listAdapter.getView(0, null, listView);
			listItem.measure(0, 0);

			if (count > 5) {
				totalHeight = listItem.getMeasuredHeight() * 5
						+ listView.getDividerHeight() * 4;
			} else {
				totalHeight = listItem.getMeasuredHeight() * count
						+ listView.getDividerHeight() * (count - 1);
			}
		}

		return totalHeight;
	}

}
